#!/bin/bash
# -*- coding: utf-8 -*-


file_path = "./login.txt"


# user_list = [
#     ("zhangkai", 123),
#     ("likai", 123)
# ]
#
# user_dict = {
#     "zhangkai": 123,
#     "likai": 123
# }

# def get_user():
#     """ 读文件，处理后，返回 """
#     user_list = []
#     with open(file_path, 'r', encoding='utf8') as f:
#         for i in f:
#             u, p = i.strip().split('|')
#             user_list.append((u, p))
#     print(user_list)
#     return user_list
#
#
# def login():
#     user_list = get_user()
#     count = 3
#     while count > 0:
#         user = input('user: ').strip()
#         pwd = input('pwd: ').strip()
#         for u, p in user_list:
#             if user == u and pwd == p:
#                 exit('登录成功')
#         else:
#             count -= 1
#             if count == 0:
#                 print('登录失败，你没有机会了')
#             else:
#                 print('登录失败，你还有[{}]次尝试机会'.format(count))
#
#
# if __name__ == '__main__':
#     login()


# def get_user():
#     """ 读文件，处理后，返回 """
#     user_dict = {}
#     with open(file_path, 'r', encoding='utf8') as f:
#         for i in f:
#             u, p = i.strip().split('|')
#             user_dict[u] = p
#     print(user_dict)
#     return user_dict
#
#
# def login():
#     user_dict = get_user()
#     count = 3
#     while count > 0:
#         user = input('user: ').strip()
#         pwd = input('pwd: ').strip()
#         if pwd == user_dict.get(user, None) and user in user_dict:
#             print('登录成功')
#             break
#         else:
#             count -= 1
#             if count == 0:
#                 print('登录失败，你没有机会了')
#             else:
#                 print('登录失败，你还有[{}]次尝试机会'.format(count))
#
#
# if __name__ == '__main__':
#     login()


def get_user():
    """ 读文件，处理后，返回 """
    user_list = []
    with open(file_path, 'r', encoding='utf8') as f:
        for i in f:
            user_list.append(i.strip())
    print(user_list)
    return user_list


def login():
    user_list = get_user()
    count = 3
    while count > 0:
        user = input('user: ').strip()
        pwd = input('pwd: ').strip()
        # print(222222, '|'.join([user, pwd]))
        if '|'.join([user, pwd]) in user_list:
            exit('登录成功')
        else:
            count -= 1
            if count == 0:
                print('登录失败，你没有机会了')
            else:
                print('登录失败，你还有[{}]次尝试机会'.format(count))


if __name__ == '__main__':  # 如果不知道这行啥意思，参考：https://www.cnblogs.com/Neeo/articles/9504779.html
    login()
